//
// RTSPSession.h
//
// $Id: //base/include/RTSPSession.h
//
// Library: base
// Package: RTSP
// Module : RTSPSession
// Author :	Jelin,doitsjz@163.com
// Date	  :	10/30/2012
// 
// Definition of the RTSPSession class.
//
// Copyright (c) 2012-2012, iGuard.
//


#ifndef RTSPSession_INCLUDED
#define RTSPSession_INCLUDED

/**
	RTSPSession class ,a session mean more pair request and response,
	in fact,same as a connection between client and server,but this 
	session implement only logic code, not include any network transport
	code,aim to for high reuse code.
*/

#include "base.h"
#include "RTSPRequest.h"
#include "RTSPResponse.h"

#include <pjlib.h>

using namespace std;


class BASE_API RTSPSession
{
public:
	RTSPSession();
	virtual ~RTSPSession();
	enum
	{
		RTSP_PORT = 554
	};
protected:
	int getCSeqID();
	void setCSeqID(int cseqID){m_cseqID = cseqID;};
	std::string generateUUID();
	std::string & getSessionID(){return m_sessionID;};
	void setSessionID(const std::string &sessionID){m_sessionID = sessionID;};

public:
	int handleMessage0(const char* buff,int iLen,pj_size_t* iLeft);

protected:
	//clientsession and serversession must implement this method
//	virtual int handleMessage(RTSPMessage* pMessage)=0;
	virtual int handleMessage(RTSPRequest& request)=0;
	virtual int handleMessage(RTSPResponse& reply)=0;

protected:
	//client side,deal with reply from server
	virtual void OnOptionsReply(int status,RTSPResponse& reply){};
	virtual void OnDescribeReply(int status,RTSPResponse& reply){};
	virtual void OnSetupReply(int status,RTSPResponse& reply){};
	virtual void OnPlayReply(int status,RTSPResponse& reply){};
	virtual void OnPauseReply(int status,RTSPResponse& reply){};
	virtual void OnRecordReply(int status,RTSPResponse& reply){};
	virtual void OnTeardownReply(int status,RTSPResponse& reply){};
	virtual void OnSetParameterReply(){};
	virtual void OnGetParameterReply(){};
	virtual void OnAnnounceReply(){};

	//server side,C->S
	virtual void OnRedirectReply(){};
	//server side,deal with request from client
	virtual void OnOptionsRequest(RTSPRequest& request){};
	virtual void OnSetParameterRequest(RTSPRequest& request){};
	virtual void OnGetParameterRequest(RTSPRequest& request){};
	virtual void OnAnnounceRequest(RTSPRequest& request){};
	virtual void OnDescribeRequest(RTSPRequest& request){};
	virtual void OnSetupRequest(RTSPRequest& request){};
	virtual void OnPlayRequest(RTSPRequest& request){};
	virtual void OnPauseRequest(RTSPRequest& request){};
	virtual void OnRecordRequest(RTSPRequest& request){};
	virtual void OnTeardownRequest(RTSPRequest& request){};

	//client side,S->C
	virtual void OnRedirectRequest(){};

public:
	int makeOptionsRequest(std::string& req);
	int makeSetParameterRequest(const std::string& req);
	int makeGetParameterRequest(const std::string& req);
	int makeAnnounceRequest(const std::string& req);

	int makeOptionsReply(RTSPRequest& request,std::string& req);
	int makeSetParameterReply(const std::string& req);
	int makeGetParameterReply(const std::string& req);
	int makeAnnounceReply(const std::string& req);
protected:
	void addCSeq(int seqid,string method);
	bool findCSeq(int seqid,string& method);
private:
	std::map<int,string> m_CSeqMap;
	std::map<int,string>::iterator m_CSeqIt;	

	int m_cseqID;
	std::string m_sessionID;
	RTSPRequest  m_Request;
	RTSPResponse m_Reply;
};

#endif
